/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ipcc.manager.entity;

import com.telsoft.applet.util.StringUtil;
import java.io.Serializable;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Date;
import telsoft.admin.util.AppServer;
import telsoft.sql.Database;

/**
 *
 * @author Administrator
 */
public class PARTNER implements Serializable,Cloneable {

    private int partner_id;
    private String partner_code;
    private String partner_name;
    private String status;
    private String create_date;

    public PARTNER() {
    }

    public int getPartner_id() {
        return partner_id;
    }

    public void setPartner_id(int partner_id) {
        this.partner_id = partner_id;
    }

    public String getPartner_code() {
        return partner_code;
    }

    public void setPartner_code(String partner_code) {
        this.partner_code = partner_code;
    }

    public String getPartner_name() {
        return partner_name;
    }

    public void setPartner_name(String partner_name) {
        this.partner_name = partner_name;
    }

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    public String getCreate_date() {
        return create_date;
    }

    public void setCreate_date(String create_date) {
        this.create_date = create_date;
    }

    public static ArrayList<PARTNER> getAll(String condition) throws Exception {
        ArrayList<PARTNER> list = new ArrayList<PARTNER>();
        Connection cn = null;
        Statement st = null;
        ResultSet rs = null;
        String strSQL = "select partner_id,partner_code,partner_name,status,to_char(create_date,'dd-mm-yyyy') from partners where 1 = 1 " + condition;
        try {
            cn = AppServer.getConnection();
            st = cn.createStatement();
            rs = st.executeQuery(strSQL);
            while (rs.next()) {
                PARTNER p = new PARTNER();
                p.setPartner_id(Integer.parseInt(rs.getString(1)));
                p.setPartner_code(rs.getString(2));
                p.setPartner_name(rs.getString(3));
                p.setStatus(rs.getString(4));
                p.setCreate_date(rs.getString(5));
                list.add(p);
            }
        } finally {
            Database.closeObject(rs);
            Database.closeObject(st);
            Database.closeObject(cn);
        }
        return list;
    }

    public String getNameById() throws Exception {
        Connection cn = null;
        PreparedStatement pst = null;
        ResultSet rs = null;
        String strSQL = "select partner_name from partners where partner_id = ?";
        try {
            cn = AppServer.getConnection();
            pst = cn.prepareStatement(strSQL);
            pst.setInt(1, getPartner_id());
            rs = pst.executeQuery();
            if (rs.next()) {
                return rs.getString(1);
            }
        } finally {
            Database.closeObject(rs);
            Database.closeObject(pst);
            Database.closeObject(cn);
        }
        return "";
    }

    public boolean insert() throws Exception {
        Connection cn = null;
        PreparedStatement pst = null;
        try {
            cn = AppServer.getConnection();
            String id = Database.getSequenceValue(cn, "seq_common");
            setPartner_id(Integer.parseInt(id));
            Date d = new Date();
            setCreate_date(StringUtil.format(d, "dd-MM-yyyy"));
            String strSQL = " insert into partners(partner_id,partner_code,partner_name,status,create_date) "
                    + " values(?,?,?,?,to_date(?,'dd-mm-yyyy')) ";
            pst = cn.prepareStatement(strSQL);

            pst.setInt(1, getPartner_id());
            pst.setString(2, getPartner_code());
            pst.setString(3, getPartner_name());
            pst.setInt(4, Integer.parseInt(getStatus()));
            pst.setString(5, getCreate_date());

            return pst.executeUpdate() > 0;
        } finally {
            Database.closeObject(pst);
            Database.closeObject(cn);
        }
    }

    public boolean remove() throws Exception {
        Connection cn = null;
        PreparedStatement stmt = null;

        String strSQL = " delete from partners where partner_id = ? ";
        try {
            cn = AppServer.getConnection();
            stmt = cn.prepareStatement(strSQL);
            
            stmt.setInt(1, getPartner_id());

            return stmt.executeUpdate() > 0;
        } finally {
            Database.closeObject(stmt);
            Database.closeObject(cn);
        }
    }

    public boolean update() throws Exception {
        Connection cn = null;
        PreparedStatement pst = null;
        try {
            cn = AppServer.getConnection();
            String strSQL = " update partners"
                    + " set partner_code = ?,partner_name = ?, status =? "
                    + " where partner_id = ? ";
            pst = cn.prepareStatement(strSQL);

            pst.setString(1, getPartner_code());
            pst.setString(2, getPartner_name());
            pst.setInt(3, Integer.parseInt(getStatus()));
            pst.setInt(4, getPartner_id());

            return pst.executeUpdate() > 0;
        } finally {
            Database.closeObject(pst);
            Database.closeObject(cn);
        }
    }
    
    @Override
    public Object clone() throws CloneNotSupportedException{
        return super.clone();
    }
}